<?php

require_once 'Base.php';

class MesscodeModel extends Base {
	
	public function getuser($mobile,$smscode){
	
		$this->_db = $this->getAdapter();
		$select = $this->_db->select();
		$select->from("shop_user")
		->where('mobile = ?', $mobile);
	     $user= $this->_db->fetchRow($select);
		return $user;
		//print_r($smsCodeend); die();
	
	
	}
	
	public function getcode($mobile,$smscode){
		
		$this->_db = $this->getAdapter();
		$select = $this->_db->select();
		$select->from($this->_table)
		->where('mobile = ?', $mobile)
	    ->where('smscode = ?', $smscode)
		->order("id desc");
		$smsCodeend= $this->_db->fetchRow($select);
		return $smsCodeend;
		//print_r($smsCodeend); die();
		
		
	}
	
	public function sendcode($mobile){
		//require_once IA_ROOT . "/web/common/XuanWuSms.php";
		
		
		
		$smsCode=rand(1111,9999);
		
		//$smsCodeend=$_COOKIE[smsCodeend];
		$this->_db = $this->getAdapter();
		$select = $this->_db->select();
		$select->from($this->_table)
		->where('mobile = ?', $mobile)
		->order("id desc");
		
		$smsCodeend= $this->_db->fetchRow($select);
		//print_r($smsCodeend); die();
		//$smscode=$select = $this->_db->select();
		
		if(!$smsCodeend || ($smsCodeend['ctime'] < time() )){
// 			setcookie('smsCode',md5(md5($smsCode)),time()+3600);
// 			setcookie('mobile',$mobile,time()+3600);
// 			setcookie('smsCodeend',time()+59,time()+59);
		
			$data['smscode']=$smsCode;
			$ctime=$data['ctime']=time()+3600;
			$data['mobile']=$mobile;
			
			
			
			
			Yaf_Loader::import('XuanWuSms.php');
			//发送短信
		//	$content="您的验证码为：".$smsCode;
		// $res=$this->doSendSMSSpeed($mobile,$content);
		
		  $content="您的验证码为:".$smsCode.'【逸儒科技】';
		  $xwSms = new \XuanWuSms("AA00388","AA0038806");
		  $res=$xwSms->_postSingle($mobile,$content);
		  //print_r($res);
		  if($res['successCounts']==1){
		  	//return array('mobile'=>$mobile,'code'=>$smsCode,'ctime'=>$ctime);
		  	$this->_db = $this->getAdapter();
		  	$this->_db->insert($this->_table,$data);
		  	$id=$this->_db->lastInsertId();
		  	return array('status'=>1,'info'=>'发送成功');
		  }else{
		  	return array('status'=>0,'info'=>'发送失败',$res);
		  }
		}else{
			//echo('操作太过频繁');
			//exit("-1");
			return array('status'=>0,'info'=>'操作太过频繁','smscode'=>$smsCodeend['smscode']);
			//return array('mobile'=>$mobile,'code'=>$smsCodeend['smscode'],'ctime'=>$smsCodeend['ctime']);
		}
	}
	
	
	
	public function getList($where = []){
		//print_r($where); die();
		//$where['id = ?'] = 1;
		if($where) $this->where($where);
		
		//$limit = "2,0";
		if($limit) $this->limit($limit);
		
		
		return $this->select();
	}
}
